package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.ItemType_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;

public class ItemTypeController extends Controller {
    private final FormFactory formFactory;
    private final ItemType_Repository tItemTypeRepository;
    private final HttpExecutionContext ec;

    @Inject
    public ItemTypeController(FormFactory formFactory, ItemType_Repository tItemTypeRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tItemTypeRepository = tItemTypeRepository;
        this.ec = ec;
    }

    public CompletionStage<Result> getTypeProduct() {
        JsonNode json = request().body().asJson();
        String ItemType = json.findPath("ItemType").textValue();

        return tItemTypeRepository.getTypeProduct(ItemType).thenApplyAsync(_tItemTypeRepository -> {
            return ok(Json.toJson(_tItemTypeRepository));
        }, ec.current());
    }
}
